package com.fudan.backend.controller;

import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.StoreModifyRequest;
import com.fudan.backend.service.StoreModifyRequestService;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/store")
public class StoreModifyRequestController {
    @Autowired
    StoreModifyRequestService storeModifyRequestService;

    @PostMapping("/modify-request")
    public R addStoreModifyRequest(@RequestBody StoreModifyRequest storeModifyRequest){
        RetCode retCode = storeModifyRequestService.addStoreModifyRequest(storeModifyRequest);
        if(retCode != RetCode.STORE_MODIFY_REQUEST_SUCCESS){
            return new R(retCode.getCode(), retCode.getMessage());
        }else{
            return new R(retCode.getCode(),retCode.getMessage(),retCode.getMessage());
        }
    }

    @PostMapping("/modify-request/process")
    public R processStoreModifyRequest(@RequestBody StoreModifyRequest storeModifyRequest){
        try {
            storeModifyRequestService.processStoreModifyRequest(storeModifyRequest);
        }catch (Exception e){
            throw  new BaseException(RetCode.STORE_MODIFY_REQUEST_FAILURE);
        }
        return new R(RetCode.STORE_MODIFY_REQUEST_SUCCESS);
    }

    @GetMapping("/modify-request")
    public R getAllStoreModifyRequest(int current, int size){
        PageInfo<StoreModifyRequest> page;
        try {
            page = storeModifyRequestService.getAllStoreModifyRequest(current, size);
        }catch (Exception e){
            throw  new BaseException(RetCode.STORE_MODIFY_REQUEST_FAILURE);
        }
        Map<String, Object> ret = new HashMap<>();
        ret.put("storeModifyRequestList", page.getList());
        ret.put("total", page.getTotal());
        return new R(RetCode.STORE_MODIFY_REQUEST_SUCCESS,ret);
    }

    @GetMapping("/modify-request/{modifyRequestId}")
    public R getStoreModifyRequestByStoreModifyRequestId(@PathVariable("modifyRequestId") String storeModifyRequestId){
        StoreModifyRequest storeModifyRequest = storeModifyRequestService.getStoreModifyRequestByStoreModifyRequestId(storeModifyRequestId);
        return new R(RetCode.STORE_MODIFY_REQUEST_SUCCESS, storeModifyRequest);
    }
}
